package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @项目: health_parent
 * @包名: com.itheima.health.controller
 * @
 * @作者: long rui nan
 * @日期: 2021-01-17 18:02
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;


    /**
     * 检查组的分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        // 调用服务 分页查询
        PageResult<Role> pageResult = roleService.findPage(queryPageBean);
        return new Result(true, "角色分页查询成功",pageResult);
    }


    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissions, Integer[] menus){

        // 调用服务 添加角色
        roleService.add(role,permissions,menus);
        return new Result(true, "角色添加成功");
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @PostMapping("/deleteById")
    public Result deleteById(int id){
        // 调用服务删除
        roleService.deleteById(id);
        return new Result(true, "角色删除成功");
    }

    /**
     * 根据id查询角色信息
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(int id){
        Role role = roleService.findById(id);
        return new Result(true,"根据id查询角色成功",role);
    }

    /**
     * 根据角色id查询角色与权限的关联id数组
     * @param id
     * @return
     */
    @GetMapping("/findPermissionsByRoleId")
    public Result findPermissionsByRoleId(int id){
        List<Integer> permissionIdList = roleService.findPermissionsByRoleId(id);
        return new Result(true,"查询角色与权限的关联id数组成功",permissionIdList);
    }

    /**
     * 根据角色id查询角色与菜单的关联id数组
     * @param id
     * @return
     */
    @GetMapping("/findMenuByRoleId")
    public Result findMenuByRoleId(int id){
        List<Integer> menuIdList = roleService.findMenuByRoleId(id);
        return new Result(true,"查询角色与权限的关联id数组成功",menuIdList);
    }
    /**
     * 修改角色
     * @param role
     * @param permissions
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Role role, Integer[] permissions, Integer[] menus){
        // 调用服务 添加角色
        roleService.update(role, permissions,menus);
        return new Result(true, "角色修改成功");
    }
}

